﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;

namespace Stationery_Store_Inventory_Management
{
    public partial class DisbursementDetailsList : System.Web.UI.Page
    {
        DisbursementListController dListCon = new DisbursementListController();
        String empId;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region Check if user exists and has permission to view this page
                if (Session["UserLoggedIn"] != null)
                {
                    List<string> list = Session["UserLoggedIn"] as List<string>;
                    if (list[0].Equals("validuser"))
                    {
                        int roleId = Convert.ToInt32(list[6]);
                        if (roleId != 5)
                        {
                            Response.Redirect("/AccessDenied.aspx");
                        }
                        empId = list[4]; //get the current employee
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/Login.aspx");
                }


                #endregion

                string DName = Request.QueryString["nm"];
                String ColPointName = Request.QueryString["ColPointName"];
                DateTime collectionDate = dListCon.getCollectionDateCont(DName);
                txtDate.Text = collectionDate.ToShortDateString();
                txtDeptName.Text = DName;
                txtCollectionPoint.Text = ColPointName;
                DateTime collectionTime = dListCon.getColPointTimeCont(ColPointName);
                txtCollectionTime.Text = collectionTime.ToShortTimeString();
                txtRepName.Text = dListCon.getRepNameCont(DName);

                // Block the buttons to prevent multiple clicks by the user
                btnConfirmDisbursement.Attributes.Add("onclick", "this.disabled=true;" + GetPostBackEventReference(btnConfirmDisbursement).ToString() + ";");
            }
            catch (Exception ex)
            {
                // Release block button
                btnConfirmDisbursement.Enabled = true;
            }
        }

        public IEnumerable<Object> DisbursementDetailListGrid_GetData()
        {
            String DeptCode = Request.QueryString["ic"];
            var disbursementDetails = dListCon.getDisbursementDetailsListCont(DeptCode);
            return disbursementDetails;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                String DeptCode = Request.QueryString["ic"];
                dListCon.updDisbursementDetailsCont(DeptCode, empId);
                btnConfirmDisbursement.Visible = false;
                Response.Write("Disbursement Details have been updated");
            }
            catch (Exception ex)
            {
                // Release block button
                btnConfirmDisbursement.Enabled = true;
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("DisbursementList.aspx");
            }
            catch (Exception ex)
            {
            }
        }

       
    }
}